/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.PasswordEncrypter;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Calendar;
import it.polimi.se2.meteocal.entity.Users;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.ConstraintViolationException;
import static org.hamcrest.CoreMatchers.is;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;


/**
 *
 * @author Ugo
 */
@RunWith(Arquillian.class)
public class UserManagerIT {
    
    public UserManagerIT() {
    }
    
    
    @EJB
    UserManager userManager;
   
    @EJB
    CalendarManager calendarManager;
   
    @PersistenceContext
    EntityManager em;
   
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(UserManager.class)
                .addClass(CalendarManager.class)
                .addPackage(Users.class.getPackage())
                .addPackage(Calendar.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
   
    @Test
    public void UserManagerShouldBeInjected() {
        assertNotNull(userManager);
    }
   
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(em);
    }

    @Test
    public void newUsersShouldBeValid() {
        Users newUser = new Users();
        newUser.setEmail("notvalidmail");
        try {
            userManager.save(newUser);
        } catch (Exception e) {
            assertTrue(e.getCause() instanceof ConstraintViolationException);
        }
       
        assertNull(em.find(Users.class, "notvalidemail"));
    }
   
    @Test
    @InSequence(1)
    public void passwordsEncryptionOnDB() {
        Users newUser = new Users();
        String password = "password";
        newUser.setEmail("test6@test.it");
        newUser.setName("Ugo");
        newUser.setCity("CITY");
        newUser.setSurname("De Vos");
        newUser.setPassword(password);
        userManager.save(newUser);
        Users foundUser = userManager.findUserByEmail("test6@test.it");
        
        assertNotNull(foundUser);
        assertThat(foundUser.getPassword(),is(PasswordEncrypter.encryptPassword(password)));
    }
   
    @Test
    @InSequence(2)
    public void calendarPrivate(){
        Calendar cal = calendarManager.findCalendarByUser(userManager.findUserByEmail("test6@test.it"));
        assertNotNull(cal);
        assertThat(cal.isPrivate(),is(false));
       
        cal.setisPrivate(true);
        calendarManager.edit(cal);
        assertThat(cal.isPrivate(),is(true));
    }

  
}
